package mailcommon.helpers;

import java.text.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DateHelper {

    private static DateHelper instance = null;
    
    private DateFormat dateFormat = null;
    private Calendar calendar = null;
    
    private DateHelper() {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calendar = dateFormat.getCalendar();
    }
    
    public static DateHelper getInstance() {
        if (instance == null) {
            instance = new DateHelper();
        }
        return instance;
    }
    
    /**
     * @param date to be formatted
     * @return String represntation of given date in format "yyyy-MM-dd"
     */
    public String format(Date date) {
        return dateFormat.format(date);
    }
    /**
     * 
     * @param year
     * @param month numerated from 1, e. g. 2 for February
     * @param date
     * @return 
     */
    public Date createDate(int year, int month, int date) {
        calendar.set(year, month-1, date);
        Date val = calendar.getTime();
        calendar.clear();
        return val;
   }
    
   public Date now() {
       calendar.setTime(new Date());
       return calendar.getTime();
       
   }
   
   /**
    * Parse date from string in format "yyyy-MM-dd"
    * @param date
    * @return 
    */
   public Date parse(String date) {
       Date parsed = null; 
       try {
           parsed = dateFormat.parse(date);
       } catch (ParseException ex) {
           Logger.getLogger(DateHelper.class.getName()).log(Level.SEVERE, null, ex);
       }
       
       return parsed;
   }
}
